package com.ymatou.admin.mq.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.admin.mq.parameter.FindMessageBean;
import com.ymatou.admin.mq.service.FindMessageCall;
import com.ymatou.mq.api.parameter.PublishBean;
import com.ymatou.mq.api.service.PublishCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.MQCfgMongoWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 查看消息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AddNewMQ {
	private static FindMessageBean fmb;
	private static FindMessageCall fmc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加新消息队列");
	}

	@Before
	public void caseUp() {
		fmb = new FindMessageBean();
		fmc = new FindMessageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(fmc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	 @Test
	 @Category(P1.class)
	 @TestCase
	 @Ignore
	public void Tc_AddNewMQ_001() {
		Logger.start(true,"添加新消息队列成功");
		try {
			Logger.comment("addMQ");
			MQCfgMongoWapper mqc = new MQCfgMongoWapper();
			String appid = "trading";
			String Code = "trading_cfmreceiveadvannotice";
			String url = "http://trade.app.ymatou.com/api/Order/Notice";
			String MessageCfgList = mqc.getMessageCfgList(appid, Code, url);
			String pushAll = "{\"MessageCfgList\":" + MessageCfgList + "}";
			mqc.pushAllCfgInfoById(appid, pushAll);
			String seJson = "{\"Version\":" + (mqc.getOneCfgInfoById("test2").getInt("Version") + 1) + "}";
			mqc.setCfgInfoById(appid, seJson);
			mqc.changeCollection("MQ_Appdomain_Cfg");
			String items = mqc.getMQAppdomainCfgList(appid, Code);
			String pushAll2 = "{\"Items\":" + items + "}";
			mqc.pushAllCfgInfoById("ad_test2", pushAll2);
			Logger.comment("sendMsg");
			// 同步配置需要时间
			Thread.sleep(30000);
			Map m = sendMsg(appid, Code);
			// MQ异步消息 3秒批量刷新一次 by 李果
			Thread.sleep(5000);
			Logger.comment("setAppId:" + m.get("Appid").toString());
			fmb.setAppId(m.get("Appid").toString());
			Logger.comment("setCode:" + m.get("Code").toString());
			fmb.setCode(m.get("Code").toString());
			Logger.comment("setMessage:" + m.get("Msguniqueid").toString());
			fmb.setMessage(m.get("Msguniqueid").toString());
			Logger.comment("setSkip:" + 1);
			fmb.setSkip(0);
			Logger.comment("setLimit:" + 10);
			fmb.setLimit(10);
			fmc.setData(fmb);
			fmc.callService();
			Logger.comment("发送fmc报文:" + fmc.getEntityString());
			Logger.comment("返回fmc报文：" + fmc.getReturnData());
			List ldata = fmc.getReturnList();
			Logger.debug(ldata.get(0));
			Map fd = (Map) ldata.get(0);
			Logger.verifyEquals(m.get("Appid").toString(), fd.get("AppId"), "验证AppId");
			Logger.verifyEquals(m.get("Code").toString(), fd.get("Code"), "验证Code");
			Logger.verifyEquals(m.get("Msguniqueid").toString(), fd.get("MsgId"), "验证MsgId");
			Logger.verifyEquals(m.get("Ip").toString(), fd.get("Ip"), "验证Ip");
			Logger.verifyEquals(true, fd.get("Body").toString().contains(m.get("Body").toString()),
					"验证Body包含：" + m.get("Body"));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * @return {Ip=127.0.0.1, Body=49CRiyhsldDIULBnxIrj, Appid=test2,
	 *         Code=performancetest,
	 *         Msguniqueid=a179f555-b53a-4a1c-9732-3c00bcbb5ce2}
	 */
	private static Map sendMsg(String appid, String Code) {
		Map m = new HashMap<String, Object>();
		try {
			PublishBean pb = new PublishBean();
			PublishCall pc = new PublishCall();
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			m.put("Appid", appid);
			pb.setAppid(appid);
			m.put("Code", Code);
			pb.setCode(Code);
			m.put("Ip", "127.0.1.1");
			pb.setIp("127.0.1.1");
			m.put("Msguniqueid", msguniqueid);
			pb.setMsguniqueid(msguniqueid);
			m.put("Body", body);
			pb.setBody(body);
			pc.setData(pb);
			pc.callService();
			Logger.debug(pc.getEntityString());
			Logger.debug(pc.getReturnData());
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return m;
	}

	/**
	 * @return list {Ip=127.0.0.1, Body=49CRiyhsldDIULBnxIrj, Appid=test2,
	 *         Code=performancetest,
	 *         Msguniqueid=a179f555-b53a-4a1c-9732-3c00bcbb5ce2}
	 */
	private static List<Map> sendMsgs(int count) {
		List<Map> lmp = new ArrayList<Map>();
		double time = 0;
		for (int i = 0; i < count; i++) {
			Map m = new HashMap<String, Object>();
			try {
				PublishBean pb = new PublishBean();
				PublishCall pc = new PublishCall();
				String msguniqueid = TestDataManager.getRandomUUID().toString();
				String body = TestDataManager.randomStr(20);
				m.put("Appid", "test2");
				pb.setAppid("test2");
				m.put("Code", "test_20150730");
				pb.setCode("test_20150730");
				m.put("Ip", "127.0.1.1");
				pb.setIp("127.0.1.1");
				m.put("Msguniqueid", msguniqueid);
				pb.setMsguniqueid(msguniqueid);
				m.put("Body", body);
				pb.setBody(body);
				pc.setData(pb);
				pc.callService();
				Logger.debug(pc.getEntityString());
				Logger.debug(pc.getReturnData());
				time += pc.getResponseTime();
				lmp.add(m);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Logger.debug(count + "次调用sendMsg，总共消耗ResponseTime：" + time);
		return lmp;
	}
}